//
//  CarBrowseHistoryVC.m
//  New_Runway
//
//  Created by mack on 2018/3/26.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarBrowseHistoryVC.h"
#import "NRWCarCarListCell.h"
#import "BuyCarDetailVC.h"

@interface CarBrowseHistoryVC ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate,LYSideslipCellDelegate>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation CarBrowseHistoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"浏览历史";
    self.view.backgroundColor = kColorWhite();
    self.dataArray = [[FMDBInfo sharedDataBase] getAllCarLsit];
    _dataArray=(NSMutableArray *)[[_dataArray reverseObjectEnumerator] allObjects];
    

    if (self.dataArray.count == 0) {
        [self.view addDefaultViewWithText:@"暂无数据" defaultImageType:DefaultImageTypeNoData];
    }else{
        [self.view removeDefaultView];
        [self initTableview];
    }
    [self.view bringSubviewToFront:self.navigationBarView];
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = MAIN_COLOR();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"NRWCarCarListCell" bundle:nil] forCellReuseIdentifier:@"NRWCarCarListCell"];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_equalTo(self.navH);
        make.bottom.equalTo(weakSelf.view);
        make.left.right.equalTo(weakSelf.view);
    }];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 110;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NRWCarCarListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NRWCarCarListCell"];
    cell.isBuyCar = YES;
    cell.delegate = self;
    cell.carModel = self.dataArray[indexPath.row];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    Result *rt = self.dataArray[indexPath.row];
    BuyCarDetailVC *detailVC = [[BuyCarDetailVC alloc] init];
    detailVC.carID = rt.carId;
    detailVC.carModel = rt;
    detailVC.isHistory = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (BOOL)sideslipCell:(LYSideslipCell *)sideslipCell canSideslipRowAtIndexPath:(NSIndexPath *)indexPath {
 
    return YES;
    
}

- (NSArray<LYSideslipCellAction *> *)sideslipCell:(LYSideslipCell *)sideslipCell editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    kSelfWeak;
    LYSideslipCellAction *rowAction = [LYSideslipCellAction rowActionWithStyle:LYSideslipCellActionStyleNormal title:@"删除" handler:^(LYSideslipCellAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        Result *rt = weakSelf.dataArray[indexPath.row];
        [[FMDBInfo sharedDataBase] deleteCarList:rt];
        [weakSelf.dataArray removeObject:rt];
        [weakSelf.tableview reloadData];
        if (weakSelf.dataArray.count == 0) {
            [self.view addDefaultViewWithText:@"暂无数据" defaultImageType:DefaultImageTypeNoData];
        }else{
            [self.view removeDefaultView];
        }
        [sideslipCell hiddenSideslip];
        
    }];
    
    return @[rowAction];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
